<template>
  <div class="academic-container">
    <div class="database-dashboard">
      <!-- 返回按钮 -->
      <div class="return-button">
        <el-button
          type="primary"
          @click="goToAllDB"
          icon="el-icon-arrow-left"
          class="return-btn"
        >
          返回
        </el-button>
      </div>

      <!-- 数据库标题 -->
      <div class="academic-header">
        <div class="title-wrapper">
          <i class="el-icon-notebook-2"></i>
          <h1 class="academic-title">{{ dbname }}</h1>
        </div>
        <div class="decorative-line"></div>
      </div>

      <!-- 属性集合 -->
      <el-card class="academic-card" shadow="never">
        <div slot="header" class="academic-card-header">
          <span class="card-title-icon">◼</span>
          <span class="card-title-text">属性集U(Attributes Set)</span>
        </div>
        <div class="academic-tag-group">
          <div
            v-for="(attr, index) in atData"
            :key="index"
            class="academic-tag"
          >
            {{ attr.attributename }}
          </div>
        </div>
      </el-card>

      <!-- 函数依赖 -->
      <el-card class="academic-card" shadow="never">
        <div slot="header" class="academic-card-header">
          <span class="card-title-icon">◼</span>
          <span class="card-title-text">函数依赖集F(Functional Dependencies)</span>
        </div>
        <div class="dependency-group">
          <div
            v-for="(fd, index) in fdData"
            :key="index"
            class="dependency-item"
          >
            <div class="dependency-lhs">{{ formatAttributes(fd.lhs) }}</div>
            <div class="dependency-arrow">→</div>
            <div class="dependency-rhs">{{ formatAttributes(fd.rhs) }}</div>
          </div>
        </div>
      </el-card>

      <!-- 动态表格 -->
      <el-card class="academic-card" shadow="never">
        <div slot="header" class="academic-card-header">
          <span class="card-title-icon">◼</span>
          <span class="card-title-text">BCNF数据库实例(Database Instance Data)</span>
        </div>
        <el-table
          :data="dynamicData"
          style="width: 100%"
          border
        >
          <el-table-column
            v-for="(column, index) in dynamicColumns"
            :key="index"
            :prop="column.name"
            :label="column.name"
          />
        </el-table>
      </el-card>

      <!-- 一致性检查 -->
      <div class="consistency-section">
        <el-button 
          type="primary" 
          class="consistency-btn"
          @click="Consistency_Check"
        >
          <i class="el-icon-document-checked"></i>
          执行一致性检查
        </el-button>

        <!-- 检查结果 -->
        <el-card 
          v-if="resultData" 
          class="academic-card result-card"
          shadow="never"
        >
          <div slot="header" class="academic-card-header">
            <span class="card-title-icon">◼</span>
            <span class="card-title-text">一致性检查结果(Consistency Verification)</span>
          </div>
          <div class="result-content">
            <div class="verdict">
              <i :class="resultIcon" :style="{color: resultColor}"></i>
              <span :style="{color: resultColor}">{{ resultText }}</span>
            </div>
            <div class="log-section">
              <h3>检查日志 Execution Logs:</h3>
              <pre class="log-content">{{ processedLogs }}</pre>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { dbdetails, showinstance} from '@/api/db';
import { consistency_check } from '@/api/consistency';
export default {
  name: 'AcademicDB',
  created() {
    this.read();
    this.showInstanceData();
  },
  computed: {
    ...mapGetters([
      'token',
      'DBid'
    ]),
    resultIcon() {
      return this.resultData?.consistent 
        ? 'el-icon-success' 
        : 'el-icon-error';
    },
    resultColor() {
      return this.resultData?.consistent 
        ? '#67C23A' 
        : '#F56C6C';
    },
    resultText() {
      return this.resultData?.consistent 
        ? '数据库满足一致性约束 (Consistency Verified)' 
        : '存在不一致性冲突 (Consistency Violation Detected)';
    },
    processedLogs() {
      return this.resultData?.logs?.join('\n') || '暂无日志信息';
    }
  },
  data() {
    return {
      dbData: [],
      dbname: '',
      fdData: [],
      atData: [],
      dynamicColumns: [],
      dynamicData: [],
      resultData: null
    };
  },
  methods: {
    read() {
      dbdetails(this.DBid).then(response => {
        this.dbname = response.data.dbname;
        this.atData = response.data.at;
        this.fdData = response.data.fd;
      }).catch(error => {
        console.error(error);
        this.$message.error('Database information retrieval failed');
      });
    },
    formatAttributes(str) {
      return str.split(',').join(', ');
    },
    showInstanceData() {
      showinstance(this.DBid).then(response => {
        const entity = response.data.entity;
        this.dynamicColumns = entity.columns.map(col => ({
          name: col.name,
          type: col.type
        }));
        this.dynamicData = entity.data;
      }).catch(error => {
        console.error(error);
        this.$message.error('Database instance data retrieval failed');
      });
    },
    goToAllDB() {
      this.$router.push('/DBII/alldb');
    },
    Consistency_Check(){
      consistency_check(this.DBid).then(response => {
        this.resultData = response.data.result;
        this.$message({
          message: '一致性检查完成',
          type: this.resultData.consistent ? 'success' : 'error',
          duration: 3000
        });
      }).catch(error => {
        console.error(error);
        this.$message.error('一致性检查请求失败');
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.academic-container {
  padding: 40px 30px;
  background: #f8f9fa;
  min-height: 100vh;
  font-family: 'Helvetica Neue', Arial, sans-serif;

  .database-dashboard {
    max-width: 1000px;
    margin: 0 auto;
    position: relative;
  }

  .return-button {
    position: absolute;
    right: 20px;
    z-index: 10;
  }

  .academic-header {
    margin-bottom: 40px;
    padding: 0 20px;

    .title-wrapper {
      display: flex;
      align-items: center;
      margin-bottom: 12px;

      .el-icon-notebook-2 {
        font-size: 36px;
        color: #2c3e50;
        margin-right: 16px;
      }

      .academic-title {
        margin: 0;
        font-size: 28px;
        color: #2c3e50;
        font-weight: 600;
        letter-spacing: 0.5px;
      }
    }

    .decorative-line {
      height: 2px;
      background: linear-gradient(90deg, #3498db 0%, rgba(52, 152, 219, 0.2) 100%);
    }
  }

  .academic-card {
    margin-bottom: 30px;
    border: 1px solid #e0e0e0;
    border-radius: 4px;
    background: #ffffff;

    ::v-deep .el-card__header {
      padding: 20px 24px;
      background: #f8fafc;
      border-bottom: 1px solid #e0e0e0;
    }

    .academic-card-header {
      display: flex;
      align-items: center;

      .card-title-icon {
        color: #3498db;
        margin-right: 12px;
        font-size: 18px;
      }

      .card-title-text {
        font-size: 18px;
        color: #2c3e50;
        font-weight: 500;
        letter-spacing: 0.3px;
      }
    }

    ::v-deep .el-card__body {
      padding: 24px;
    }

    &.result-card {
      margin-top: 20px;
      border-color: #e0e0e0;
      
      .result-content {
        .verdict {
          display: flex;
          align-items: center;
          margin-bottom: 20px;
          padding: 15px;
          background: #f8fafc;
          border-radius: 4px;

          i {
            font-size: 24px;
            margin-right: 12px;
          }

          span {
            font-size: 16px;
            font-weight: 500;
          }
        }

        .log-section {
          h3 {
            color: #606266;
            margin-bottom: 12px;
            font-size: 14px;
          }

          .log-content {
            padding: 15px;
            background: #f8fafc;
            border-radius: 4px;
            max-height: 300px;
            overflow: auto;
            font-family: Consolas, monospace;
            font-size: 13px;
            line-height: 1.6;
            color: #2c3e50;
            border: 1px solid #e0e0e0;

            &::-webkit-scrollbar {
              width: 6px;
            }

            &::-webkit-scrollbar-thumb {
              background: #c0c4cc;
              border-radius: 4px;
            }
          }
        }
      }
    }
  }

  .consistency-section {
    text-align: center;
    margin-top: 30px;

    .consistency-btn {
      padding: 12px 28px;
      font-size: 16px;
      letter-spacing: 0.5px;
      background: #3498db;
      border-color: #3498db;
      transition: all 0.3s;

      &:hover {
        background: #2980b9;
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(52, 152, 219, 0.3);
      }

      i {
        margin-right: 8px;
      }
    }
  }

  .academic-tag-group {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;

    .academic-tag {
      padding: 8px 18px;
      background: #f0f4f8;
      border: 1px solid #d0d7de;
      border-radius: 4px;
      color: #2c3e50;
      font-size: 14px;
      font-family: 'Courier New', monospace;
      letter-spacing: 0.5px;
      transition: all 0.2s;

      &:hover {
        background: #e3f2fd;
        border-color: #3498db;
      }
    }
  }

  .dependency-group {
    display: grid;
    gap: 16px;

    .dependency-item {
      display: flex;
      align-items: center;
      padding: 16px;
      background: #f8fafc;
      border: 1px solid #e0e0e0;
      border-radius: 4px;

      .dependency-lhs,
      .dependency-rhs {
        padding: 8px 16px;
        background: #ffffff;
        border: 1px solid #d0d7de;
        border-radius: 4px;
        font-family: 'Courier New', monospace;
        font-size: 15px;
        color: #2c3e50;
      }

      .dependency-arrow {
        margin: 0 16px;
        font-size: 20px;
        color: #3498db;
        font-weight: 500;
      }
    }
  }
}
</style>